//
//  ChangePwdViewController.m
//  YunGou
//
//  Created by macbook on 16/5/30.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "UIBarButtonItem+Extension.h"

@interface ChangePwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPwd;
@property (weak, nonatomic) IBOutlet UITextField *NewPwd;
@property (weak, nonatomic) IBOutlet UITextField *NewPwdAgain;
@property (weak, nonatomic) IBOutlet UILabel *nowAccountLabel;
@end

@implementation ChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *rightBarItem = [UIBarButtonItem barButtonTitle:@"确认" image:nil frame:CGRectMake(0, 0, 30, 44) target:self sel:@selector(OkClicked)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    NSMutableString *mobile = [NSMutableString stringWithString:[UserModel sharedInstance].mobile];
    [mobile replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    _nowAccountLabel.text = [NSString stringWithFormat:@"当前账户：%@",mobile];
}

- (void)OkClicked{
    NSString *oldPsw = [UserDefaults objectForKey:@"password"];
    if (_oldPwd.text.length == 0) {
        [MBProgressHUD showError:@"请输入原密码"];
    }else if (_NewPwd.text.length == 0){
        [MBProgressHUD showError:@"请输入新密码"];
    }else if (![_NewPwd.text isEqualToString:_NewPwdAgain.text]){
        [MBProgressHUD showError:@"两次密码不一致"];
    }else if (![_oldPwd.text isEqualToString:oldPsw]){
        [MBProgressHUD showError:@"原密码输入有误"];
    }else if ([_NewPwd.text isEqualToString:oldPsw]){
        [MBProgressHUD showError:@"新旧密码不能一致"];
    }else{
        [NetWorkTool changePwdWithMobile:[UserModel sharedInstance].mobile oldPwd:_oldPwd.text newPwd:_NewPwd.text success:^(id object) {
            if ([object[@"code"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"修改成功"];
                [UserDefaults setObject:_NewPwd.text forKey:@"password"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MBProgressHUD showError:@"原密码输入错误"];
            }
        } failure:^(NSError *error) {
            FZLog(@"%@",error);
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
